from django.contrib import admin

# Register your models here.
from cases.models import InterfaceInfo, Case, SqlScript, Steps, TeSuite, VerifyResult, ResultParamGet, CommonParamGet
from cases.views import run_test_suits, run_test_case, run_test_step


@admin.register(TeSuite)
class TeSuitAdmin(admin.ModelAdmin):
    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）
    list_display = ('id','suit_name', 'description', 'entype','is_run')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)

    filter_horizontal = ('case',)
    # list_editable 设置默认可编辑字段
    list_editable = ['description', 'is_run']

    # fk_fields 设置显示外键字段
    fk_fields = ('system_name',)

    def run_test_suit(modeladmin, request, queryset):
        run_test_suits(queryset)

    run_test_suit.short_description = '运行选中的案例集'

    actions = [run_test_suit]
    # 自动生成该表下的created_user字段数据
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_user = request.user  # request.user为当前登录用户
        obj.save()




@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    # fields = ('sql_content','sql_type')
    #exclude = ('sql_content','sql_type','response_body','response_header','method')
    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）

    list_display = ('case_name', 'case_description', 'system_name', 'is_run')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)
    # list_editable 设置默认可编辑字段
    list_editable = ['case_description', 'is_run']

    # fk_fields 设置显示外键字段
    fk_fields = ('system_name',)

    def run_test_case(modeladmin, request, queryset):
        run_test_case(queryset)

    run_test_case.short_description = '运行选中的案例'

    actions = [run_test_case]
    # 自动生成该表下的created_user字段数据
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_user = request.user  # request.user为当前登录用户
        obj.save()

@admin.register(VerifyResult)
class VerifyResultAdmin(admin.ModelAdmin):
    # fields = ('sql_content','sql_type')
    #exclude = ('sql_content','sql_type','response_body','response_header','method')
    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）

    list_display = ('step','area', 'actualPath','rule',  'exceptValue')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)
    # list_editable 设置默认可编辑字段
    list_editable = ['area', 'rule', 'actualPath', 'exceptValue']
    fk_fields = ('step',)
    # 自动生成该表下的created_user字段数据
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_user = request.user  # request.user为当前登录用户
        obj.save()


@admin.register(SqlScript)
class SqlScriptAdmin(admin.ModelAdmin):
    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）
    list_display = ('sql_name', 'sql_content','sql_type')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)

    # list_editable 设置默认可编辑字段
    list_editable = ['sql_content','sql_type' ]

    # fk_fields 设置显示外键字段
    fk_fields = ('sql_name',)
    # 自动生成该表下的created_user字段数据
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_user = request.user  # request.user为当前登录用户
        obj.save()


class ResultParamGetInline(admin.TabularInline):

    model = ResultParamGet
    fk_name = "step"
    extra = 1

class VerifyResultInline(admin.TabularInline):

    model = VerifyResult
    fk_name = "step"
    extra = 1



@admin.register(Steps)
class StepsAdmin(admin.ModelAdmin):
    inlines = [VerifyResultInline,ResultParamGetInline]
    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）
    list_display = ('id', 'step_description', 'case_name','operation_type', 'interface_name', 'replace_param')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)

    # list_editable 设置默认可编辑字段
    list_editable = ['step_description', 'replace_param']

    # fk_fields 设置显示外键字段
    fk_fields = ('id',)
    #筛选器
    list_filter =('case_name','sql_name') #过滤器
    def run_test_step(modeladmin, request, queryset):
        run_test_step(queryset)

    run_test_step.short_description = '运行选中的步骤'

    actions = [run_test_step]

    # autocomplete_fields=['case_name']

    # 自动生成该表下的created_user字段数据
    def save_model(self, request, obj, form, change):
        if not change:
            obj.created_user = request.user  # request.user为当前登录用户
        obj.save()

    fieldsets = (
        ('基本信息', {
            'fields': (('case_name', 'step_description'), ('operation_type','is_run'))}
                ),
        ('接口信息', {
                    'classes': ('collapse','extrapretty'),  # 'collapse','wide', 'extrapretty'
                    'fields': (('interface_name'),('request_header','is_use_temp'),('request_body','replace_param'))
                }),
        ('数据库信息', {
            'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'
            'fields': ('sql_name','common_param'),
        }),
        # ('结果处理信息', {
        #     'classes': ('collapse', 'wide', 'extrapretty'),  # 'collapse','wide', 'extrapretty'
        #     'fields': ('expected_result', 'get_res_header_param','get_res_param',),
        # })
    )
    raw_id_fields = ("interface_name",)

@admin.register(CommonParamGet)
class CommonParamAdmin(admin.ModelAdmin):

    # listdisplay设置要显示在列表中的字段（id字段是Django模型的默认主键）
    list_display = ('type','key','value')

    # list_per_page设置每页显示多少条记录，默认是100条
    list_per_page = 50

    # ordering设置默认排序字段，负号表示降序排序
    ordering = ('-created_time',)

    # list_editable 设置默认可编辑字段
    list_editable = ['key', 'value']

    # fk_fields 设置显示外键字段
    fk_fields = ('key',)
    #筛选器
    list_filter =('key', 'type') #过滤器
    search_fields =('suite', 'case', 'type', 'key','value') #搜索字段


# admin.site.register(InterfaceInfo,InterfaceAdmin)
# admin.site.register(Case)
# admin.site.register(SqlScript)
# admin.site.register(Steps)
admin.site.site_header = '测试管理平台'
admin.site.site_title = '测试管理平台'
